# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=ngtcp2
pkgver=1.4.0
pkgrel=0
pkgdesc="Implementation of IETF QUIC protocol"
url="https://nghttp2.org/ngtcp2/"
arch="all"
license="MIT"
makedepends="
	cmake
	gnutls-dev
	samurai
	"
checkdepends="cunit-dev"
subpackages="
	$pkgname-dev
	$pkgname-gnutls
	"
source="https://github.com/ngtcp2/ngtcp2/releases/download/v$pkgver/ngtcp2-$pkgver.tar.xz"

build() {
	# Build static library separately w/o LTO for compatibility with non-gcc
	# and minimal size.
	_build build-static -DENABLE_SHARED_LIB=OFF

	# NOTE: -DINTERPROCEDURAL_OPTIMIZATION=ON doesn't work here.
	export CFLAGS="$CFLAGS -flto=auto"
	_build build-shared -DENABLE_STATIC_LIB=OFF
}

_build() {
	local outdir="$1"; shift

	local crossopts=
	[ "$CBUILD" != "$CHOST" ] \
		&& crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"

	cmake -B "$outdir" -G Ninja \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DENABLE_GNUTLS=ON \
		$crossopts \
		"$@"
	cmake --build "$outdir"
}

check() {
	cmake --build build-shared -t check
}

package() {
	DESTDIR="$pkgdir" cmake --install build-static
	DESTDIR="$pkgdir" cmake --install build-shared

	rm -rf "$pkgdir"/usr/share/doc
}

gnutls() {
	pkgdesc="$pkgdesc - GnuTLS backend"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/lib/libngtcp2_crypto_gnutls.so*
}

sha512sums="
6491f158cd3bd659d593810935ceedefea8ee4a77e0a1952c0300f83188d0af1ef8313b24f9dbd3e7a795b6ad9099d2681b70ff1bafd92eed429ee028b2f2dea  ngtcp2-1.4.0.tar.xz
"
